<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">
        <el-form-item>
          <el-input v-model="selectOptions.plant_name" placeholder="植物名" @keyup.enter.native="doSearch"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="doSearch">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" icon="plus" style="width: 120px"  @click="addPlant">添加种子</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="background: #13ce66" v-on:click="_reload">刷新</el-button>
        </el-form-item>

        <div style="display: inline-block;float: right">
          <el-form-item>
            <el-button type="danger" v-on:click="batchModifyPrice" v-show="batchModify">批量修改价格</el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table ref="multipleTable" :data="seeds" v-loading="loading" border tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange" element-loading-text="玩命请求中。。。">
      <el-table-column label="名称" width="80" prop="name">
      </el-table-column>
      <el-table-column label="列表图片" width="120" prop="pic">
        <template slot-scope="scope">
          <a :href="scope.row.list_pic" target="_blank"><img :src="scope.row.list_pic" alt="" style="width: 80px; height: 50px;margin-top: 2px;"></a>
        </template>
      </el-table-column>
      <el-table-column label="详情图片" width="300">
        <template slot-scope="scope">
          <div style="overflow: hidden;">
            <a v-for="(item, index) in scope.row.detail_pic" :href="item" target="_blank"><img :src="item" alt="" style="width: 80px; height: 50px;margin-right: 2px;margin-top: 2px;"></a>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="售价(元)" prop="price" :formatter="changePrice">
      </el-table-column>
      <el-table-column label="标价(元)" prop="orgin_price" :formatter="changeorginPrice">
      </el-table-column>
      <el-table-column label="库存(份)" prop="inventory">
      </el-table-column>
      <el-table-column label="产量(斤)" prop="production" :formatter="changeProduction">
      </el-table-column>
      <el-table-column label="浇水cd(时)" prop="plant_water_interval" :formatter="waterToHour">
      </el-table-column>
      <el-table-column label="施肥cd(时)" prop="plant_fert_interval" :formatter="fertToHour">
      </el-table-column>
      <el-table-column label="除草cd(时)" prop="plant_weed_interval" :formatter="weedToHour">
      </el-table-column>
      <el-table-column label="除虫cd(时)" prop="plant_bug_interval" :formatter="bugToHour">
      </el-table-column>
      <el-table-column label="成熟期(天)"  prop="maturity">
      </el-table-column>

      <el-table-column label="权重"  prop="sort" width="120">
        <template slot-scope="scope">
          <plantSortInput :val="scope.row.sort" :plant_name="scope.row.name"></plantSortInput>
        </template>
      </el-table-column>

      <!--<el-table-column prop="create_time" label="创建时间" width="170">-->
      <!--</el-table-column>-->
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.row.name)" msg="scope.row">编辑</el-button>
          <el-button type="danger" size="small" @click="deletePlant(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--批量修改价格-->
    <el-dialog title="批量修改价格" v-model="updatePriceShow" :close-on-click-modal="false">
      <el-form :model="singlePlantData" label-width="80px" ref="addForm">

        <el-form-item label="蔬菜名" prop="plant_name">
          <template slot-scope="scope">
            <select v-model="singlePlantData.plant_name" style="width: 100px;height: 30px;" @change="changeSelectPlant">
              <option value="">请选择</option>
              <option :value="item" v-for="(item, index) in plants">{{ item }}</option>
            </select>
          </template>
        </el-form-item>

        <el-form-item label="售价" prop="price">
          <template slot-scope="scope">
            <div style="width: 300px;display: inline-block;">
              <el-input v-model="singlePlantData.price" auto-complete="on" readonly></el-input>
            </div>
            <span style="margin-left: 10px;">例如：59(元)-输入数字</span>
          </template>
        </el-form-item>

        <el-form-item label="标价" prop="orgin_price">
          <template slot-scope="scope">
            <div style="width: 300px;display: inline-block;">
              <el-input v-model="singlePlantData.orgin_price" auto-complete="on" readonly></el-input>
            </div>
            <span style="margin-left: 10px;">例如：59(元)-输入数字</span>
          </template>
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click.native="updatePriceShow = false">取消</el-button>
        <el-button type="primary" @click.native="batchModifyPriceSubmit">提交</el-button>
      </div>
    </el-dialog>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="currentPageChange"
        :page-size="30"
        layout="prev, pager, next, jumper"
        :total="total" style="float:right;" :current-page="parseInt(selectOptions.page)">
      </el-pagination>
      <span style="float: right;margin-top: 6px;margin-right: 5px">共 {{total}} 条数据</span>
    </el-col>

  </section>
</template>
<script type="text/ecmascript-6">
  import util from '../../util'
  import NProgress from 'nprogress'
  import plantSortInput from '../common/plantSortInput'
  import {isUrlHasPurview} from '../../vuex/getters'
  import { apiTree } from '../../apiTree'

  export default {
    name: 'plant',
    // 局部注册组件
    components: {
      plantSortInput
    },
    data() {
      return {
        plant_name: '',
        editFormVisible: false,
        loading: false,
        editLoading: false,
        seeds: [],
        total: 0,
        updatePriceShow: false,
        singlePlantData: {
          plant_name: '',
          price: 0,
          orgin_price: 0
        },
        plants: [],
        editPlantForm: {
          type: 2,
          name: '',
          desc: '',
          list_pic: '',
          detail_pic: [],
          production: 0, // 产量
          maturity: 0, // 成熟期
          price: 0, // 单价
          inventory: 0 // 库存
        },
        selectOptions: {
          page: this.$route.query.page !== undefined ? this.$route.query.page : 1,
          plant_name: this.$route.query.plant_name !== undefined ? this.$route.query.plant_name : ''
        }
      }
    },
    computed: {
      // 编辑管理员信息按钮是否显示
      batchModify() {
        return !util.isEmpty(isUrlHasPurview(this.$store.state, 'plant/batchModifyPrice'))
      }
    },
    methods: {
      // 该表单价
      changePrice(row) {
        return parseInt(row.price) / 100
      },
      // 浇水cd转化为小时
      waterToHour(row) {
        return parseInt(row.plant_water_interval) / 3600
      },
      // 施肥cd转化为小时
      fertToHour(row) {
        return parseInt(row.plant_fert_interval) / 3600
      },
      // 除草cd转化为小时
      weedToHour(row) {
        return parseInt(row.plant_weed_interval) / 3600
      },
      // 除虫cd转化为小时
      bugToHour(row) {
        return parseInt(row.plant_bug_interval) / 3600
      },
      changeorginPrice(row) {
        return parseInt(row.orgin_price) / 100
      },
      batchModifyPrice() {
        this.updatePriceShow = true;
        this.getPlantName();
      },
      changeSelectPlant() {
        let param = {name: this.singlePlantData.plant_name};
        apiTree('plant/batchModifyPrice', 'field/getSinglePlantInfo', param).then((res) => {
          if (res.s === 1) {
            this.singlePlantData.price = parseInt(res.d.price) / 100;
            this.singlePlantData.orgin_price = parseInt(res.d.orgin_price) / 100;
          }
        });
      },
      // 批量修改单价和价格
      batchModifyPriceSubmit() {
        if (util.isEmpty(this.singlePlantData.plant_name)) {
          this.$message.error('参数错误');
          return;
        }
        if (this.singlePlantData.price === 0 || this.singlePlantData.orgin_price === 0 || this.singlePlantData.orgin_price < this.singlePlantData.price) {
          this.$message.error('售价标价错误');
          return;
        }
        apiTree('plant/batchModifyPrice', 'plant/batchModifyPrice', this.singlePlantData, 'POST').then((res) => {
          if (res.s === 1) {
            this.$message({
              message: '提交成功',
              type: 'success'
            });
            this.updatePriceShow = false;
          } else if (res.s < 1) {
            this.$message.error(res.err_str)
          }
        });
      },
      // 获得植物名
      getPlantName() {
        this.plants = []
        apiTree('plant/batchModifyPrice', 'field/getAllPlantName', {}).then((res) => {
          if (res.s === 1) {
            res.d.data.forEach((item) => {
              this.plants.push(item.name);
            })
          }
        })
      },
      // 改变产量
      changeProduction(row) {
        return Math.floor(parseInt(row.production) / 500)
      },
      // 选中列
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      currentPageChange(val) {
        this.selectOptions.page = val
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      },
      doSearch() {
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      },
      // 获得种子列表
      getSeeds() {
        let para = this.$route.query
        if (util.isEmptyObject(para)) {
          this.selectOptions = {
            page: 1,
            plant_name: ''
          }
        }
        this.loading = true
        NProgress.start()
        apiTree('plant/index', 'plant/index', para).then((res) => {
          if (res.s === 1) {
            this.total = res.d.total_num
            this.seeds = res.d.data
            this.loading = false
            NProgress.done()
          }
        })
      },
      // 显示编辑界面
      handleEdit: function (_name) {
        this.$router.push('/plant/edit/name/' + _name)
      },
      // 编辑种子
      editPlant(index, row) {
        this.editFormVisible = true
        this.editPlantForm = {
          type: 2,
          name: row.name,
          desc: row.detail_desc,
          list_pic: row.list_pic,
          detail_pic: row.detail_pic,
          production: parseInt(row.production) / 500, // 产量
          maturity: row.maturity, // 成熟期
          price: parseInt(row.price) / 100, // 单价
          inventory: row.inventory // 库存
        }
      },
      // 删除种子
      deletePlant(row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.loading = true
          NProgress.start()
          let param = {name: row.name}
          apiTree('plant/index', 'plant/delete', param).then((res) => {
            if (res.s === 1) {
              this.loading = false;
              NProgress.done()
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.getSeeds()
            } else {
              this.$message.error(res.err_str)
            }
          })
        }).catch(() => {})
      },
      // 刷新列表
      _reload() {
        this.getSeeds();
      },
      // 增加种子
      addPlant() {
        this.$router.push('/plant/add')
      }
    },
    created() {
      this.getSeeds();
      this.$on('getSeeds', () => {
        this.getSeeds();
      })
    },
    watch: {
      $route: 'getSeeds'
    }
  }

</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .p-pic-old
    display inline-block
    width 500px
  .pl-pic-upload
    display inline-block
    margin-left 10px
    position relative
  .p-pic-edit
    left: 175px;
    top: -44px;
    .upload_plant_pic
      display block
      width 80px
      height 35px
      cursor pointer
      position absolute
      top 0px
      right 20px
      opacity 0
      & input[type=file]
        display block
        cursor pointer
        width 80px
        height 35px
  .p-pl-poster
    width 160px
    height 80px
    margin-bottom 20px
    margin-top 0px
    & img
      width 100%
      height 100%
  .el-form-item
    margin-bottom 10px
</style>
